package com.zsh.wanandroid.base;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.kingja.loadsir.core.LoadService;
import com.zsh.wanandroid.common.APP;
import com.zsh.wanandroid.common.AppManager;
import com.zsh.wanandroid.ui.activity.LoginActivity;
import com.zsh.wanandroid.utils.UIUtils;

import me.jessyan.autosize.AutoSizeCompat;

/**
 * @author zsh
 * @date 2019/10/18.
 * 功能：Activity基类，实现了点击事件接口、网络监听接口、GPS监听接口、实体按键监听接口...
 */

public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener {

    public BaseActivity self = BaseActivity.this;
    private long exitTime = 0;//记录时间

    public ProgressDialog progressDialog;
    public LoadService loadService;
    public boolean isLogin;

    /**
     * 当前Activity渲染的视图View
     **/
    private View mContextView = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //添加Activity到堆栈，必须放在doBusiness之前
        AppManager.getAppManager().addActivity(this);

        Bundle bundle = getIntent().getExtras();
        initPrams(bundle);
        View mView = bindView();
        if (null == mView) {
            mContextView = LayoutInflater.from(this)
                    .inflate(bindLayout(), null);
        } else {
            mContextView = mView;
        }
        setContentView(mContextView);
        initView(mContextView);//初始化控件
        setListener();//设置监听
        doBusiness(savedInstanceState);
    }

    /**************************基本方法****************************/
    /**
     * [初始化参数]
     *
     * @param prams
     */
    public abstract void initPrams(Bundle prams);

    /**
     * [绑定视图]
     *
     * @return
     */
    public abstract View bindView();

    /**
     * [绑定布局]
     *
     * @return
     */
    public abstract int bindLayout();

    /**
     * [初始化控件]
     *
     * @param view
     */
    public abstract void initView(final View view);

    /**
     * [绑定控件]
     *
     * @param resId
     * @return
     */
    protected <T extends View> T $(int resId) {
        return (T) super.findViewById(resId);
    }

    /**
     * [设置监听]
     */
    public abstract void setListener();

    /**
     * [View点击]
     **/
    public abstract void widgetClick(View v);

    /**
     * [业务操作]
     *
     * @param savedInstanceState
     */
    public abstract void doBusiness(Bundle savedInstanceState);

    /**
     * [点击事件]
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        widgetClick(v);
    }

    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(BaseActivity.this, clz));
    }

    public void startActivityForResult(Class<?> clz, int requestCode) {
        startActivityForResult(new Intent(BaseActivity.this, clz), 0);
    }

    /**
     * [携带数据的页面跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * [退出程序]
     */
    public boolean exitApp() {
        //判断2次点击事件时间
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            UIUtils.showToast("再按一次退出程序");
            exitTime = System.currentTimeMillis();
            return false;
        } else {
            return true;
        }
    }

    /**
     * [简化Toast]
     *
     * @param msg
     */
    protected void showToast(String msg) {
        Toast.makeText(self, msg, Toast.LENGTH_SHORT).show();
    }

    public void startLoginActivity() {
        startActivity(LoginActivity.class);
    }

    @Override
    protected void onResume() {
        super.onResume();
        isLogin = SPUtils.getInstance(APP.currentAppName).getBoolean(APP.isLogin);
    }

    /**
     * activity销毁时操作
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
    }

}
